﻿Public Class frmUser

    Private Sub frmUser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        getHeaderGrid_User()
        reset()
        getUser()
    End Sub

    Protected Sub getHeaderGrid_User()
        Dim HeaderText As String() = {"លេខរៀង", "អ្នកប្រើប្រាស់", "លេខសម្ងាត់", "សិទ្ធ", "ឈ្មោះបុគ្គលិក", "ឈ្មោះមេធាវី"}
        Dim HeaderWidth As Integer() = {100, 200, 200, 200, 300, 300}
        Dim i As Integer
        dgvData.ColumnCount = HeaderText.Count
        For i = 0 To HeaderText.Count - 1
            dgvData.Columns(i).HeaderText = HeaderText(i)
            dgvData.Columns(i).Width = HeaderWidth(i)
        Next
    End Sub

    Public Sub getUser()
        dgvData.Rows.Clear()
        Dim oUser As New cUser
        Dim dt_Lawyer, dt_Employee As New DataTable
        dt_Lawyer = oUser.select_User_Lawyer()
        dt_Employee = oUser.select_User_Employee()
        Dim dr_Lawyer, dr_Employee As DataRow
        Dim i As Integer = 1
        For Each dr_Lawyer In dt_Lawyer.Rows
            dgvData.Rows.Add(i, dr_Lawyer(0), dr_Lawyer(1), dr_Lawyer(2), "", dr_Lawyer(3))
            i += 1
        Next
        For Each dr_Employee In dt_Employee.Rows
            dgvData.Rows.Add(i, dr_Employee(0), dr_Employee(1), dr_Employee(2), dr_Employee(3), "")
            i += 1
        Next
    End Sub

    Protected Sub reset()
        txtSearch.Clear()
        txtSearch.Focus()
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Close()
    End Sub

    Private Sub ShowFormWithoutClose(ByVal frm As Form)
        frm.MdiParent = Me.MdiParent
        frm.ControlBox = False
        frm.ShowIcon = False
        frm.WindowState = FormWindowState.Maximized
        frm.Show()
        frm.BringToFront()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        ShowFormWithoutClose(frmUserEntry)
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        If dgvData.Rows.Count > 0 Then
            If dgvData.CurrentRow.Cells(4).Value <> "" Then
                frmUserEntry._isEmployee = True
            Else
                frmUserEntry._isEmployee = False
            End If
            frmUserEntry._UserID = dgvData.CurrentRow.Cells(1).Value
            ShowFormWithoutClose(frmUserEntry)
        Else
            MessageBox.Show("គ្មានពត៌មានកែប្រែទេ!", "បង្ហាញពត៌មាន", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        deleted()
    End Sub

    Protected Sub deleted()
        Dim oUser As New cUser
        If dgvData.Rows.Count > 0 Then
            If MessageBox.Show("តើអ្នកប្រាកដជាលុប '" & dgvData.CurrentRow.Cells(1).Value & "' មែនទេ?", "លុបចោល", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                If oUser.delete_User(dgvData.CurrentRow.Cells(1).Value) Then
                    MessageBox.Show("លុបចោលបានសម្រេច!", "លុបចោល", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    getUser()
                Else
                    MessageBox.Show("លុបចោលមិនបានសម្រេចទេ", "លុបចោល", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            End If
        Else
            MessageBox.Show("គ្មានពត៌មានសម្រាប់់លុបទេ!", "លុបចោល", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub dgvData_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvData.DoubleClick
        btnUpdate_Click(Nothing, Nothing)
    End Sub

    Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click
        If dgvData.Rows.Count > 0 Then
            If dgvData.CurrentRow.Cells(4).Value <> "" Then
                frmUserEntry._isEmployee = True
            Else
                frmUserEntry._isEmployee = False
            End If
            frmUserEntry._UserID = dgvData.CurrentRow.Cells(1).Value
            frmUserEntry.EnableControl()
            ShowFormWithoutClose(frmUserEntry)
        Else
            MessageBox.Show("គ្មានពត៌មានសម្រាប់់បង្ហាញទេ!", "បង្ហាញពត៌មាន", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
        getSearch()
    End Sub

    Protected Sub getSearch()
        dgvData.Rows.Clear()
        Dim oUser As New cUser
        Dim dt_Lawyer, dt_Employee As DataTable
        dt_Lawyer = oUser.select_User_Lawyer_byKeyword(txtSearch.Text)
        dt_Employee = oUser.select_User_Employee_byKeyword(txtSearch.Text)
        Dim dr_Lawyer, dr_Employee As DataRow
        Dim i As Integer = 1
        For Each dr_Lawyer In dt_Lawyer.Rows
            dgvData.Rows.Add(i, dr_Lawyer(0), dr_Lawyer(1), dr_Lawyer(2), "", dr_Lawyer(3))
            i += 1
        Next
        For Each dr_Employee In dt_Employee.Rows
            dgvData.Rows.Add(i, dr_Employee(0), dr_Employee(1), dr_Employee(2), dr_Employee(3), "")
            i += 1
        Next
    End Sub
End Class